package com.zlx.store.order.controller;

import com.zlx.store.common.constant.RespCode;
import com.zlx.store.common.results.HttpResp;
import com.zlx.store.domain.entity.vo.GoodsVO;


import com.zlx.store.domain.entity.vo.ShoppingCartVO;
import com.zlx.store.domain.entity.vo.TokenVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Api("tags=用户的Api接口类")
@RestController
@RequestMapping("api/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取token
     * @param request
     * @return
     */
    private TokenVO getTokenVo(HttpServletRequest request) {
        String token = request.getHeader("token");
        return (TokenVO) redisTemplate.opsForValue().get(token);
    }
    /**
     * 由于购物车直接存入redis，再考虑到为了查看，
     * 目前在redis中的数据都是string格式，所以与redis交互的vo使用对象属性用的全是string
     * 这里前端比较累，需要将购物车商品进行包装后传入后台
     * 由于购物车会将相同购物车进行合并修改，所以删除和修改合为一体
     * 一个人只有一个购物车，所所以不存在批量删除的问题，
     * 另外一个人肯定会有一个购物车，如果没找到直接就是空，所以也不用有其他接口了
     * 添加购物车
     * @return
     */
    @PutMapping("update")
    @ApiOperation(value = "updateShoppingCart", notes = "更新购物车")
    @ApiImplicitParam(name = "ShoppingCartVO", required = true)
    public HttpResp updateShoppingCart(@RequestBody ShoppingCartVO shoppingCartVO, HttpServletRequest request) {
        TokenVO tokenVO = getTokenVo(request);
        redisTemplate.opsForValue().set("shoppingCart"+tokenVO.getUserId(),shoppingCartVO);
        return HttpResp.httpResp(RespCode.SHOPPING_CART_OPERATION_SUCCESS,null,new Date());
    }

    /**
     * 查找自己的购物车
     *由于只有redis的购物车所以直接从redis中取
     * @param request
     * @return
     */
    @GetMapping("find")
    @ApiOperation(value = "findShoppingCart", notes = "查找我的购物车")
    public HttpResp findShoppingCart(HttpServletRequest request) {
        TokenVO tokenVO = getTokenVo(request);
        ShoppingCartVO shoppingCartVO = (ShoppingCartVO) redisTemplate.opsForValue().get("ShoppingCart:" + tokenVO.getUserId());
        return HttpResp.httpResp(RespCode.SHOPPING_CART_OPERATION_SUCCESS,shoppingCartVO,new Date());
    }
    /**
     * 向购物车添加商品
     */
    @PostMapping("add")
    @ApiOperation(value = "add", notes = "向购物车添加物品")
    @ApiImplicitParam(name = "addShoppingCart",required = true)
    private HttpResp addShoppingCart(HttpServletRequest request,GoodsVO goodsVO){
        if (Objects.isNull(goodsVO))return HttpResp.httpResp(RespCode.ITEM_OPERATION_ERROR,"商品不可为空",new Date());
        TokenVO tokenVO = getTokenVo(request);
        ShoppingCartVO shoppingCartVO = (ShoppingCartVO) redisTemplate.opsForValue().get("ShoppingCart:" + tokenVO.getUserId());
        if (Objects.isNull(shoppingCartVO)){
            List<GoodsVO> goodsVOList=new ArrayList<>();
            goodsVOList.add(goodsVO);
            ShoppingCartVO cartVO = new ShoppingCartVO();
            cartVO.setGoodsVOList(goodsVOList);
            redisTemplate.opsForValue().set("ShoppingCart"+tokenVO.getUserId(),cartVO);
            return HttpResp.httpResp(RespCode.SHOPPING_CART_OPERATION_SUCCESS,shoppingCartVO,new Date());
        }
        shoppingCartVO.getGoodsVOList().add(goodsVO);
        redisTemplate.opsForValue().set("shoppingCart"+tokenVO.getUserId(),shoppingCartVO);
        return HttpResp.httpResp(RespCode.SHOPPING_CART_OPERATION_SUCCESS,shoppingCartVO,new Date());
    }
}
